package com.tencent.mars.xlogsample;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;


import com.tencent.mars.xlog.Xlog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LogcatReader {
    private static final int MSG_READ_LOGCAT = 1;
    private HandlerThread mHandlerThread;
    private Handler mHandler;

    public LogcatReader() {
        mHandlerThread = new HandlerThread("LogcatReaderThread");
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == MSG_READ_LOGCAT) {
                    try {
                        Process process = Runtime.getRuntime().exec("logcat -d");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                        String line;
                        while ((line = reader.readLine())!= null) {
                            // 在这里可以对logcat的日志进行处理，然后再通过xlog记录
                            //2024-11-12 23:42:57.517  3602-4047  DollieAdapterService    com.hihonor.systemserver             E  notifyActivityState pkg:com.tencent.mars.xlogsample/com.tencent.mars.xlogsample.MainActivity state:2 fg:true mUid:10041
                            //2024-11-12 23:42:57.518 15398-15398 PhoneWindow             com.tencent.mars.xlogsample          D  onViewRootImplSet activityToken android.os.BinderProxy@d5afe17,activityClientRecord ActivityRecord{c101bce token=android.os.BinderProxy@d5afe17 {com.tencent.mars.xlogsample/com.tencent.mars.xlogsample.MainActivity}},isHwNavigationHide false isHidePrivateFlag = false
                            String temp = line;
                            LogInfo logInfo = LogcatToXLog.convertLogcatToLogInfo(temp);
                            if (logInfo!=null){
                                Xlog.logWrite2(logInfo.getLevel(),
                                        logInfo.getTag(),
                                        logInfo.getFilename(),
                                        logInfo.getFuncname(),
                                        logInfo.getLine(),
                                        logInfo.getPid(),
                                        logInfo.getTid(),
                                        logInfo.getMaintid(),
                                        logInfo.getLog()
                                );
                            }
                        }

                        reader.close();
                        process.destroy();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
    }

    public void startReading() {
        mHandler.sendEmptyMessageDelayed(MSG_READ_LOGCAT,6000);
    }

    public void stopReading() {
        mHandler.removeMessages(MSG_READ_LOGCAT);
        mHandlerThread.quitSafely();
    }
}